<template>
	<view class="">
		<view class="top-fix">
			<view class="search" >
				<u--input style="width: 100%" placeholder="请输入企业名称" v-model="enterpriseName"/>
				<!-- <u--input placeholder="请选择" disabled shape="circle" prefixIcon="search"
				 prefixIconStyle="font-size: 22px;color: #909399" /> -->
				<view class="btn-filter" @click.stop="showFilter=!showFilter">
					<text>筛选</text>
					<u-icon :name="showFilter?'arrow-up-fill':'arrow-down-fill'" color="#B2B2B2"/>
				</view>
			</view>
			<view v-show="showFilter" style="padding: 20upx;">
				<u--form labelPosition="top" ref="queryForm" labelWidth="auto" :labelStyle="{
					fontSize: '14px',
					color: '#B2B2B2',
					lineHight: '20px'
				}">
					<!-- <u-form-item label="企业名称">
						<u--input style="width: 100%" placeholder="请输入企业名称" v-model="enterpriseName"/>
					</u-form-item> -->
					<u-form-item label="商户类型">
						<h-select-input-tag
						style="width: 100%"
						  v-model="officialType"
						  placeholder="请选择商户类型"
						  dictName="official_type"
							suffixIcon="arrow-down-fill"
							:suffixIconStyle="{ color: '#B2B2B2' }"/>
						
					</u-form-item>
				</u--form>
				<view style="display: flex;">
					<u-button type="primary" style="margin-right: 5upx;" :plain="true" text="重置" @click="resetForm"/>
					<u-button type="primary" style="margin-left: 5upx;" text="确定" @click="downCallback"/>
				</view>
			</view>
		</view>
		
		<mescroll-body ref="mescrollRef" :top='showFilter?270:80' bottom='130' @init="mescrollInit" @down="downCallback" :up="upOption"
		@up="upCallback">
			
			<view class="list-center">
				<view class="appList" v-for="(item,index) in dataList" :key='index' @click="chooseTo(item)">
					<view class="option">
					<view class="lf">企业名称</view>
					<view class="rl">{{ item.enterpriseName }}</view>
					</view>
					<view class="option">
					<view class="lf">商户类型</view>
					<view class="rl">{{ item.officialType_dictText }}</view>
					</view>
					<view class="option">
					<view class="lf">企业信用代码</view>
					<view class="rl">{{ item.enterpriseCreditCode }}</view>
					</view>
					<view class="option">
					<view class="lf">企业地址</view>
					<view class="rl">{{ item.enterpriseAddress }}</view>
					</view>
					<view class="option">
					<view class="lf">企业类型</view>
					<view class="rl">{{ item.enterpriseType_dictText }}</view>
					</view>
					<view class="option">
					<view class="lf">注册时间</view>
					<view class="rl">{{ item.registerDate }}</view>
					</view>
					<view class="option">
					<view class="lf">注册资本</view>
					<view class="rl">{{ item.registerCapital }}</view>
					</view>
					<view class="option">
					<view class="lf">授信等级</view>
					<view class="rl">{{ item.creditRating_dictText }}</view>
					</view>
					<u-icon class="choose" v-if="item.choosed" name="checkbox-mark" color="#2979ff" size="22"/>
				</view>
			</view>
			
		</mescroll-body>
		<view class="bottom-area flex_column_center">
			<u-button style="width: 90%;" type="primary" text="确定" @click="sureTo"/>
		</view>
	</view>
	
</template>

<script>
import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js'
import { erpPurchaseSupplierList } from '@/api/index.js'
import APPTYPE from '@/config/appType.js';
export default{
	name:'HSelectSupplierInput',
	mixins: [MescrollMixin], // 使用mixin
	data(){
		return{
			showFilter: false,
			isProPayContract:true,
			isNoJudge: false,
			enterpriseName:'',//企业名称
			officialType:'0',//商户类型
			dataList:[],
			selectObj:{},//当前选中的对象
			upOption: {
				textNoMore: '',
				noMoreSize: 10,
				empty: {
					btnText: '',
					tip: '暂无相关数据'
				}
			},
		}
	},
	onLoad(options) {
		
		this.isProPayContract = options.isProPayContract?false:true
		this.isNoJudge = options.isNoJudge?true:false
		
	},
	methods:{
		resetForm(){
			this.enterpriseName = ''
			this.officialType = '0'
			
			this.downCallback()
		},
		downCallback() {
			// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			this.$nextTick(() => {
				this.mescroll.resetUpScroll()
			})
		},
		// 更新供应商列表
		getList () {
			this.downCallback()
		},
		getTotla () {},
		
		async upCallback(page){
			
			let params = {
				column: 'createTime',
				order: 'desc',
				pageNo: page.num,
				pageSize: page.size,
				commercialType: '2',
				officialType: this.officialType,
				enterpriseName: this.enterpriseName?`*${this.enterpriseName}*`:''
			}
			
			let res = await erpPurchaseSupplierList(params)
			
			if (res.success) {
				const {
					records,
					total
				} = res.result
				
				if (page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
				this.dataList = this.dataList.concat(records); //追加新数据
				
				for (let it of this.dataList) {
					it.choosed = false
				}
				
				// 结束加载,传当前页长度和总页数
				this.mescroll.endByPage(records.length, total);
			}
			
		},
		// 单选选择
		chooseTo(record){
			if (this.isNoJudge) {
				this.updateChoose(false,record)
			} else {
				if(this.isProPayContract){
				                    
				  if(record.cooperationNum==3&&record.officialType==0){
				  
					this.updateChoose(true,record)
					this.getDailog('该供应商已与我们多次合作，请继续完善信息！',record)
					
				  }else if(record.officialType==1&&record.auditStatus==0){
					
					this.updateChoose(true,record)
					this.getDailog('该供应商审核未通过，请继续完善信息！',record)
					
				  } else if(record.officialType==1&&record.auditStatus==1){
					
					this.updateChoose(true,record)
					return this.$u.toast('该供应商在审核中！')
				  }else{
					this.updateChoose(false,record)
				  }
					
				}else{
					if(record.officialType==1&&record.auditStatus==0){
						this.updateChoose(true,record)
						this.$u.toast('该供应商审核未通过，请继续完善信息！')
					}else if(record.officialType==1&&record.auditStatus==1){
				
						this.updateChoose(true,record)
				
						return this.$u.toast('该供应商在审核中！')
					}else if(record.officialType==0&&record.cooperationNum==3){
				
						this.updateChoose(true,record)
						this.$u.toast('该供应商已与我们多次合作，请继续完善信息！')
				
					} else{
						this.updateChoose(false,record)
					}
				}
			}
			
		},
		// 更新选择是否
		updateChoose(rest,record){
			for (let it of this.dataList) {
				if(rest){
					it.choosed = false
				}else{
					if(it.id===record.id){
						it.choosed=true
						this.selectObj = it
					}else{
						it.choosed=false
					}
				}
				
			}
			this.$forceUpdate()
		},
		// 弹框选择
		getDailog(content,record){
			uni.showModal({
				title: '温馨提示',
				content: content,
				cancelText: "取消", // 取消按钮的文字  
				confirmText: "确认", // 确认按钮的文字  
				confirmColor: '#f55850',
				cancelColor: '#39B54A',
				success: (res) => {
					if (res.confirm) {
						// 跳转到供应商编辑页面填写信息
						uni.navigateTo({
							url: `/pages/purchase/ErpPurchaseSupplierForm`+`?formType=${APPTYPE.EDIT}&id=${record.id}&isProPayContract=1`
						})
						
					} else {
						console.log('cancel') //点击取消之后执行的代码
					}
				}
			})
			
		},
		// 确定
		sureTo(){
			uni.$emit("supplierBack",this.selectObj)
			uni.navigateBack()
		}
	}
}

</script>

<style lang="scss" scoped>
.top-fix{
	width: 100%;
	background-color: #FFFFFF;
	position: fixed;
	/* #ifdef APP-PLUS */
	top: 0;
	/* #endif */
	/* #ifdef H5 */
	top:44px;
	/* #endif */
	z-index: 999;
}
.search {
	margin-top: 1rpx;
	display: flex;
	padding: 20upx;
	.btn-filter {
		margin-left: 20rpx;
		display: flex;
		align-items: center;
		color: #7F7F7F;
	}

}
.list-center {
	widht: 100%;
	background: #F3F3F7;
	border-radius: 30upx;
	margin: 0 20upx;
	.appList{
		position: relative;
		background: #FFFFFF;
		margin-top: 20upx;
		padding: 20upx;
		font-weight: 400;
		.choose{
			position: absolute;
			right: 10upx;
			top: 45%;
		}
	}
}
.bottom-area{
	position: fixed;
	bottom: 0px;
	height: 120upx;
	background: #FFFFFF;
	width: 100%;
}
.flex_column_center {
	  display: flex;
	  flex-direction: column;
	  flex-wrap: nowrap;
	  justify-content: center;
	  align-items: center;
	}
</style>